package Demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 殇
 * Date: 2025-07-11
 * Time: 19:17
 */
public class Test {
    //冒泡排序
    public static void bubble(Comparable[] comparable){
        int n=comparable.length;
        for(int i=0;i<n;i++){
            for(int j=0;j<n-1-i;j++){
                if(comparable[j].compareTo(comparable[j+1])>0){
                    Comparable tmp=comparable[j];
                    comparable[j]=comparable[j+1];
                    comparable[j+1]=tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student stu1=new Student("red",18);
        Student stu2=new Student("blue",20);
        Student stu3=new Student("green",17);
        Student[] array=new Student[3];
        array[0]=stu1;
        array[1]=stu2;
        array[2]=stu3;

        System.out.println("排序前:"+Arrays.toString(array));

        bubble(array);
        System.out.println("冒泡排序:"+Arrays.toString(array));
    }
    public static void main2(String[] args) {
        Student stu1=new Student("red",18);
        Student stu2=new Student("blue",20);
        Student stu3=new Student("green",17);
        Student[] array=new Student[3];
        array[0]=stu1;
        array[1]=stu2;
        array[2]=stu3;

        System.out.println("排序前:"+Arrays.toString(array));

        Arrays.sort(array);
        System.out.println("Comparable排序:"+Arrays.toString(array));

        NameComparator nameComparator=new NameComparator();
        Arrays.sort(array,nameComparator);
        System.out.println("Comparator排序:"+Arrays.toString(array));



    }
    public static void main1(String[] args) {
        Student stu1=new Student("red",18);
        Student stu2=new Student("blue",20);
        if(stu1.compareTo(stu2)<0)
            System.out.println("stu1<stu2");

        System.out.println("=============================");
        NameComparator nc1=new NameComparator();
        int ret1=nc1.compare(stu1,stu2);
        if(ret1>0)
            System.out.println("stu1>stu2");
        else
            System.out.println("stu1<=sut2");
        System.out.println("=============================");
        AgeComparator nc2=new AgeComparator();
        int ret2=nc2.compare(stu1,stu2);
        if(ret2>0)
            System.out.println("stu1>stu2");
        else
            System.out.println("stu1<=sut2");
    }
}
